
import BaseComponent from "../../Game/Base/BaseClass/BaseComponent";
import DWC_MsgList from "./DWC_MsgList";
import FPJDefine from "../../Game/FPJ/Prefabs/FPJDefine";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Dwc_SimpleChat extends BaseComponent {


    @property(DWC_MsgList)
    DWC_MsgListCom: DWC_MsgList = null;

    @property(cc.Node)
    msgPlane: cc.Node = null;

    @property(cc.Float)
    showTime = 10;

    //聊天数据
    // _msgTalkList = {};
    _msgTalkList = [];

    //是否能打开快捷消息界面
    _canOpenMsgPlane = true;
    _showTime = 0;

    _lock = false;

    player_list = [];
    gameChangeMsg = null;
    onLoad() {
        this._msgTalkList = [];
        this.player_list = [];
    }
    start() {
        this.onEvent("ServerNtc");
        this.gameChangeMsg =  cc.vv.eventCustom.on("GameChange", this.GameChange.bind(this));
        cc.systemEvent.on("OPEN_SIMPLECHAT", this.openMsgPlaneNode.bind(this));
        this.msgPlane.active = false;

    }

    GameChange() {
        let playerList = FPJDefine.player_list;
        for (let i in playerList) {
            let playerItem = playerList[i];
            //如果不存在
            if (!this.checkHadPlayer(playerItem["rid"]) && playerItem["rid"] != cc.vv.session.getRid() ) {
                //通知有人进入
                let msg = {
                    tip: true,           //这个标记作为判断玩家进入游戏
                    content: " ",
                    logo: playerItem["logo"],
                    rid: playerItem["rid"],
                    rolename: playerItem["rolename"],
                }
                cc.vv.eventCustom.emit("PlayerEnter", msg);
                this.onATTChatMessageNtc(msg);
            }
        }
        this.player_list = JSON.parse(JSON.stringify(FPJDefine.player_list) );
    }

    checkHadPlayer(rid) {
        for (let i in this.player_list) {
            if (rid == this.player_list[i]["rid"]) {
                return true;
            }
        }
        return false
    }

    init(data) {
        if(FPJDefine.player_list){
            this.player_list = FPJDefine.player_list.concat([]);
        }
    }

    ServerNtc(data) {
        data = data[0];
        let handler_name = "on" + data.cmd;
        if (this[handler_name]) {
            this[handler_name](data);
        }
    }
    onATTChatMessageNtc(data) {
        if (!this._msgTalkList) this._msgTalkList = [];
        this._showTime = this.showTime;
        //this._msgTalkList.push(data);
        this._msgTalkList = [data];
        //打开消息面板，至于是否真的打开了，由这个openMsgPlane自己去管
        this.openMsgPlane();
    }

    btnCloseMsgPlane() {
        this.msgPlane.active = false;
        this._canOpenMsgPlane = false;
    }

    openMsgPlaneNode() {
        this.msgPlane.active = true;
        this._canOpenMsgPlane = true;
        this._showTime = this.showTime;
        this.openMsgPlane();
    }

    setCanOpenMsgPlane(canOpen) {
        this._canOpenMsgPlane = canOpen;
        this.openMsgPlane();
    }

    openMsgPlane() {

        if( !FPJDefine.isVipRoom() ){ return }

        //复杂聊天存在则不能打开
        if (this.checkDetailChatIsOpen()) { return; }

        if (!this._canOpenMsgPlane) { return; }

        this._lock = true;
        this._showTime = this.showTime;
        if(this.DWC_MsgListCom){
            this.DWC_MsgListCom.initData(this._msgTalkList.concat([]));
            this.msgPlane.active = true;
            this.msgPlane.opacity = 255;
        }

        //---------------------------------
        //消息数据
    }

    update(dt) {
        this._showTime -= dt;
        if (this._showTime < 0) {
            if (this._lock) {
                this._lock = false;
                // this.msgPlane.active = false;
                this.msgPlane.stopAllActions();
                cc.tween(this.msgPlane)
                    .to(1, { "opacity": 0 })
                    .call(() => {
                        this.msgPlane.active = false;
                    })
                    .start();
            }
        }
    }

    //检查复杂聊天是否存在
    checkDetailChatIsOpen() {
        let isOpen = cc.vv.UIMgr.getOpen(cc.vv.GLConfig.DWC_DetailChat_path) ? true : false;
        return isOpen;
    }

    onDestroy() {
        cc.systemEvent.off("OPEN_SIMPLECHAT", this.openMsgPlaneNode,this);
        cc.vv.eventCustom.off("GameChange", this.gameChangeMsg );
        this.offEvent("ServerNtc");
    }
}

